package com.jlrdb.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.jlrdb.model.AnalogRadioBroadcast;
import com.jlrdb.model.AnalogRadioBroadcasts;
import com.jlrdb.model.DigitalRadioBroadcast;
import com.jlrdb.model.DigitalRadioBroadcasts;
import com.jlrdb.service.RadioBroadcastService;

public class RadioBroadcastServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		RadioBroadcastService radioBroadcastService = new RadioBroadcastService();
		if (action.equals("viewRadioChoices"))
		{
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			String Role;
			String domain = "temp";
			String identity = user.getEmail();
			if (identity.contains("jaguarlandrover.com"))
				domain = "jaguarlandrover.com";
			if (domain.equalsIgnoreCase("jaguarlandrover.com"))
			{
				if (identity.equalsIgnoreCase("gagarwa2@jaguarlandrover.com") || identity.equalsIgnoreCase("rholenar@jaguarlandrover.com") || identity.equalsIgnoreCase("poreill3@jaguarlandrover.com")
						|| identity.equalsIgnoreCase("sarcot@jaguarlandrover.com"))
					Role = "Admin";
				else
					Role = "Viewer";
				HttpSession session = request.getSession();
				session.setAttribute("Role", Role);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewRadioBroadcastByChoice.jsp");
				rd.forward(request, response);
			} else
			{
				String message = "Please LOGIN with a valid JLR domain account.";
				request.setAttribute("message", message);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/message.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("addDigitalRadioBroadcast"))
		{
			DigitalRadioBroadcast digitalRadioBroadcast = new DigitalRadioBroadcast();
			digitalRadioBroadcast.setCountry(request.getParameter("country"));
			digitalRadioBroadcast.setGeographic_Region(request.getParameter("geographic_region"));
			digitalRadioBroadcast.setAnalog_radio(request.getParameter("analog_radio"));
			digitalRadioBroadcast.setDigitization_Status(request.getParameter("digitization_status"));
			digitalRadioBroadcast.setDigital_Radio_Family(request.getParameter("digital_radio_family"));
			digitalRadioBroadcast.setStandard(request.getParameter("standard"));
			digitalRadioBroadcast.setTransmission_Status(request.getParameter("transmission_status"));
			digitalRadioBroadcast.setCoverage(request.getParameter("coverage"));
			digitalRadioBroadcast.setStart_Date(request.getParameter("start_date"));
			digitalRadioBroadcast.setStop_date(request.getParameter("stop_date"));
			digitalRadioBroadcast.setNotes(request.getParameter("notes"));
			digitalRadioBroadcast.setFrequency_Spectrum(request.getParameter("frequency_spectrum"));
			digitalRadioBroadcast.setMajor_Players(request.getParameter("major_players"));
			digitalRadioBroadcast.setDigital_Radio_Overall_coverage(request.getParameter("digital_radio_overall_coverage"));
			digitalRadioBroadcast.setMultiPlex(request.getParameter("multiplex"));
			digitalRadioBroadcast.setASO_Start_Date(request.getParameter("aso_start_date"));
			digitalRadioBroadcast.setASO_End_Date(request.getParameter("aso_end_date"));
			digitalRadioBroadcast.setRegulatory_authority(request.getParameter("regulatory_authority"));
			digitalRadioBroadcast.setSource(request.getParameter("source"));
			digitalRadioBroadcast = radioBroadcastService.checkEmptyValuesDigital(digitalRadioBroadcast);
			int result = radioBroadcastService.addRadioBroadcastDigital(digitalRadioBroadcast);
			if (result == 0)
			{
				request.setAttribute("digitalRadioBroadcast", digitalRadioBroadcast);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/digitalRadioBroadcastAdded.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("addAnalogRadioBroadcast"))
		{
			AnalogRadioBroadcast analogRadioBroadcast = new AnalogRadioBroadcast();
			analogRadioBroadcast.setCountry(request.getParameter("country"));
			analogRadioBroadcast.setGeographic_Region(request.getParameter("geographic_region"));
			analogRadioBroadcast.setAm(request.getParameter("am"));
			analogRadioBroadcast.setFm(request.getParameter("fm"));
			analogRadioBroadcast.setMajor_Players(request.getParameter("major_players"));
			analogRadioBroadcast.setNotes_Remarks(request.getParameter("notes_remarks"));
			analogRadioBroadcast = radioBroadcastService.checkEmptyValuesAnalog(analogRadioBroadcast);
			int result = radioBroadcastService.addRadioBroadcastAnalog(analogRadioBroadcast);
			if (result == 0)
			{
				request.setAttribute("analogRadioBroadcast", analogRadioBroadcast);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/analogRadioBroadcastAdded.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("viewRadioBroadcasts"))
		{
			DigitalRadioBroadcasts digitalRadioBroadcasts = new DigitalRadioBroadcasts();
			AnalogRadioBroadcasts analogRadioBroadcasts = new AnalogRadioBroadcasts();
			String datachoice = request.getParameter("data_choice");
			System.out.println(request.getParameter("data_choice"));
			System.out.println(request.getParameter("country"));
			System.out.println(request.getParameter("geographic_region"));
			if (!request.getParameter("country").equals("none"))
			{
				String criteria = "country";
				String value = request.getParameter("country");
				digitalRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsDigital(criteria, value);
				analogRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsAnalog(criteria, value);
				request.setAttribute("digitalRadioBroadcasts", digitalRadioBroadcasts);
				request.setAttribute("analogRadioBroadcasts", analogRadioBroadcasts);
				request.setAttribute("geography", request.getParameter("geographic_region"));
				request.setAttribute("country", request.getParameter("country"));
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewRadioFullDetails.jsp");
				rd.forward(request, response);
			} else if (datachoice.equals("Digital"))
			{
				String criteria = "geographic_region";
				String value = request.getParameter("geographic_region");
				digitalRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsDigital(criteria, value);
				request.setAttribute("digitalRadioBroadcasts", digitalRadioBroadcasts);
				request.setAttribute("geography", request.getParameter("geographic_region"));
				RequestDispatcher rd1 = request.getRequestDispatcher("jsp/viewRadioBroadcastsDigital.jsp");
				rd1.forward(request, response);
			} else if (datachoice.equals("Analog"))
			{
				String criteria = "geographic_region";
				String value = request.getParameter("geographic_region");
				analogRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsAnalog(criteria, value);
				request.setAttribute("analogRadioBroadcasts", analogRadioBroadcasts);
				request.setAttribute("geography", request.getParameter("geographic_region"));
				RequestDispatcher rd2 = request.getRequestDispatcher("jsp/viewRadioBroadcastsAnalog.jsp");
				rd2.forward(request, response);
			}
		} else if (action.equals("getThisBroadcastDigital"))
		{
			long broadcastId = Long.parseLong(request.getParameter("broadcastId"));
			DigitalRadioBroadcast digitalRadioBroadcast = new DigitalRadioBroadcast();
			digitalRadioBroadcast = radioBroadcastService.getThisBroadcastDigital(broadcastId);
			request.setAttribute("digitalRadioBroadcast", digitalRadioBroadcast);
			RequestDispatcher rd = request.getRequestDispatcher("jsp/updateRadioBroadcastDigital.jsp");
			rd.forward(request, response);
		} else if (action.equals("getThisBroadcastAnalog"))
		{
			long broadcastId = Long.parseLong(request.getParameter("broadcastId"));
			AnalogRadioBroadcast analogRadioBroadcast = new AnalogRadioBroadcast();
			analogRadioBroadcast = radioBroadcastService.getThisBroadcastAnalog(broadcastId);
			request.setAttribute("analogRadioBroadcast", analogRadioBroadcast);
			RequestDispatcher rd = request.getRequestDispatcher("jsp/updateRadioBroadcastAnalog.jsp");
			rd.forward(request, response);
		} else if (action.equals("updateRadioBroadcastDigital"))
		{
			DigitalRadioBroadcast digitalRadioBroadcast = new DigitalRadioBroadcast();
			// digitalRadioBroadcast.setCountry(request.getParameter("country"));
			// digitalRadioBroadcast.setGeographic_Region(request.getParameter("geographic_region"));
			digitalRadioBroadcast.setAnalog_radio(request.getParameter("analog_radio"));
			digitalRadioBroadcast.setDigitization_Status(request.getParameter("digitization_status"));
			digitalRadioBroadcast.setDigital_Radio_Family(request.getParameter("digital_radio_family"));
			digitalRadioBroadcast.setStandard(request.getParameter("standard"));
			digitalRadioBroadcast.setTransmission_Status(request.getParameter("transmission_status"));
			digitalRadioBroadcast.setCoverage(request.getParameter("coverage"));
			digitalRadioBroadcast.setStart_Date(request.getParameter("start_date"));
			digitalRadioBroadcast.setStop_date(request.getParameter("stop_date"));
			digitalRadioBroadcast.setNotes(request.getParameter("notes"));
			digitalRadioBroadcast.setFrequency_Spectrum(request.getParameter("frequency_spectrum"));
			digitalRadioBroadcast.setMajor_Players(request.getParameter("major_players"));
			digitalRadioBroadcast.setDigital_Radio_Overall_coverage(request.getParameter("digital_radio_overall_coverage"));
			digitalRadioBroadcast.setMultiPlex(request.getParameter("multiplex"));
			digitalRadioBroadcast.setASO_Start_Date(request.getParameter("aso_start_date"));
			digitalRadioBroadcast.setASO_End_Date(request.getParameter("aso_end_date"));
			digitalRadioBroadcast.setRegulatory_authority(request.getParameter("regulatory_authority"));
			digitalRadioBroadcast.setSource(request.getParameter("source"));
			DateFormat df = new SimpleDateFormat("dd/MM/yy");
			String formattedDate = df.format(new Date());
			digitalRadioBroadcast.setLast_Update_Date(formattedDate);
			digitalRadioBroadcast = radioBroadcastService.checkEmptyValuesDigital(digitalRadioBroadcast);
			long broadcastId = Long.parseLong(request.getParameter("broadcastId"));
			digitalRadioBroadcast = radioBroadcastService.checkEmptyValuesDigital(digitalRadioBroadcast);
			int result = radioBroadcastService.updateRadioBroadcastDigital(digitalRadioBroadcast, broadcastId);
			if (result == 0)
			{
				AnalogRadioBroadcasts analogRadioBroadcasts = new AnalogRadioBroadcasts();
				DigitalRadioBroadcasts digitalRadioBroadcasts = new DigitalRadioBroadcasts();
				String criteria = "country";
				String value = request.getParameter("country");
				digitalRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsDigital(criteria, value);
				analogRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsAnalog(criteria, value);
				request.setAttribute("digitalRadioBroadcasts", digitalRadioBroadcasts);
				request.setAttribute("analogRadioBroadcasts", analogRadioBroadcasts);
				request.setAttribute("geography", request.getParameter("geographic_region"));
				request.setAttribute("country", request.getParameter("country"));
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewRadioFullDetails.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("updateRadioBroadcastAnalog"))
		{
			AnalogRadioBroadcast analogRadioBroadcast = new AnalogRadioBroadcast();
			// analogRadioBroadcast.setCountry(request.getParameter("country"));
			// analogRadioBroadcast.setGeographic_Region(request.getParameter("geographic_region"));
			analogRadioBroadcast.setAm(request.getParameter("am"));
			System.out.println(request.getParameter("am"));
			analogRadioBroadcast.setFm(request.getParameter("fm"));
			System.out.println(request.getParameter("fm"));
			analogRadioBroadcast.setMajor_Players(request.getParameter("major_players"));
			analogRadioBroadcast.setNotes_Remarks(request.getParameter("notes_remarks"));
			DateFormat df = new SimpleDateFormat("dd/MM/yy");
			String formattedDate = df.format(new Date());
			analogRadioBroadcast.setLast_Date_Updated(formattedDate);
			analogRadioBroadcast = radioBroadcastService.checkEmptyValuesAnalog(analogRadioBroadcast);
			long broadcastId = Long.parseLong(request.getParameter("broadcastId"));
			int result = radioBroadcastService.updateRadioBroadcastAnalog(analogRadioBroadcast, broadcastId);
			if (result == 0)
			{
				AnalogRadioBroadcasts analogRadioBroadcasts = new AnalogRadioBroadcasts();
				DigitalRadioBroadcasts digitalRadioBroadcasts = new DigitalRadioBroadcasts();
				String criteria = "country";
				String value = request.getParameter("country");
				digitalRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsDigital(criteria, value);
				analogRadioBroadcasts = radioBroadcastService.viewRadioBroadcastsAnalog(criteria, value);
				request.setAttribute("digitalRadioBroadcasts", digitalRadioBroadcasts);
				request.setAttribute("analogRadioBroadcasts", analogRadioBroadcasts);
				request.setAttribute("geography", request.getParameter("geographic_region"));
				request.setAttribute("country", request.getParameter("country"));
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewRadioFullDetails.jsp");
				rd.forward(request, response);
			}
		}
	}
}
